package com.yanggu.spock.service

import com.yanggu.spock.domain.vo.UserVO
import com.yanggu.spock.util.HttpContextUtils
import spock.lang.Specification
import spock.lang.Unroll

import static org.mockito.Mockito.mockStatic

class OrderServiceStaticTest extends Specification {

    def orderService = new OrderService()

    /**
     * 测试spock的mock和power mock静态方法组合用法的场景
     */
    @Unroll
    def "当来源是#source时，订单类型为:#type"() {
        given: "静态mock"
        def mockedStatic = mockStatic(HttpContextUtils)

        and: "mock当前上下文的请求来源和币种"
        mockedStatic.when { HttpContextUtils.getCurrentSource() }.thenReturn(source)
        mockedStatic.when { HttpContextUtils.getCurrentCurrency() }.thenReturn(currency)

        when: "调用获取用户订单列表"
        def orderList = orderService.getUserOrdersBySource(new UserVO())

        then: "验证返回结果是否符合预期值"
        with(orderList) {
            it[0].type == type
        }

        cleanup: "清理mockStatic"
        mockedStatic.close()

        where: "表格方式验证订单信息的分支场景"
        source   | currency || type
        "APP"    | "CNY"    || 1
        "APP"    | "USD"    || 1
        "WAP"    | ""       || 2
        "ONLINE" | ""       || 3
    }

}
